package com.prp.struts.action;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.opensymphony.xwork2.ActionSupport;
import com.prp.dao.Userinfo;
import com.prp.service.ServiceFactory;

public class EditUserAction extends ActionSupport {

	/**
	 * 
	 */
	
	/**
	 * @return
	 */
	private String username_sl;
	//private boolean isAdmin;
	private List auths;
	
	private Userinfo userInfo;
	
	private Set editLogList;
	
	private int pageSize = 15;
	
	private int pageNow = 1;
	
	private int pageCount;
	
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getPageNow() {
		return pageNow;
	}
	public void setPageNow(int pageNow) {
		this.pageNow = pageNow;
	}
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	
	public String getUsername_sl() {
		return username_sl;
	}

	public void setUsername_sl(String username) {
		this.username_sl = username;
	}

	public void setUserInfo(Userinfo userInfo) {
		this.userInfo = userInfo;
	}

	public Userinfo getUserInfo() {
		return userInfo;
	}

	public void setEditLogList(Set editLogList) {
		this.editLogList = editLogList;
	}

	public Set getEditLogList() {
		return editLogList;
	}

	public void setAuths(List auths) {
		this.auths = auths;
	}

	public List getAuths() {
		return auths;
	}
	
	
	
	
	
	public String editUser()
	{
		
		if(ServiceFactory.getAdminSystemSer().editUserAuth(userInfo.getUsername(), userInfo.getIsAdmin(), auths))
			return SUCCESS;
		else 
			return INPUT;
		
		
	}
	
	public String editUserPage()
	{
		
		userInfo = ServiceFactory.getAdminSystemSer().getUser(username_sl);
		if (userInfo == null)
		{
			return ERROR;
		}
		
		auths = ServiceFactory.getAdminSystemSer().getAuths(userInfo);
			
		Set editLogAll = userInfo.getEditlogs();
		
		pageCount = editLogAll.size();
		
		if(pageNow <= 0)
			pageNow = 1;
		
		if(pageCount%pageSize == 0){
			if(pageNow > pageCount/pageSize)
				pageNow = pageCount / pageSize;
		}else{
			if(pageNow > pageCount/pageSize + 1)
				pageNow = pageCount / pageSize + 1;
		}
		
		if(editLogList != null)
			editLogList.clear();
		else
			editLogList = new HashSet();
		
		Iterator it = editLogAll.iterator();
		
		int count = 0;
		while(it.hasNext()){
			if(count < pageSize*pageNow && count >= pageSize*(pageNow - 1)){
				editLogList.add(it.next());
				
			}
			count++;
			if (count > pageSize*pageNow)
				break;
			
		}
		
		return "edituserpage";
	}
	
	public String execute() {
				
		return SUCCESS;
		
	}

	





	
}